/**
 * Title:        矩阵还是显示E7的样式检测
 * Company:      泛微软件
 */


package checkDetail;

import general.BaseBean;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.json.JSONObject;

import weaver.conn.RecordSet;
import weaver.general.Util;
import weaver.matrix.MatrixUtil;
import weaver.rtx.RTXClientCom;
import weaver.workflow.workflow.GroupDetailMatrix;
import weaver.workflow.workflow.GroupDetailMatrixDetail;



public class MatrixSynchronize extends BaseBean implements CheckBase  {

    @Override
    public JSONObject checkData(HashMap dataMap) {
        setRecordSet();
        (new BaseBean()).writeLog("HtmlInfoCheck  start");

        RTXClientCom rtxClient = new RTXClientCom();
        RecordSet rs = new RecordSet();
        RecordSet rs1 = new RecordSet();
        RecordSet rslog = new RecordSet();
        RecordSet temprs = new RecordSet();
        List resultList = new ArrayList();
        String result = "";
        List<String[]> argslst = new ArrayList<String[]>();

        argslst = MatrixUtil.getMatrixIDAndFieldID();

        String matrixid = "";
        String matrixFieldid = "";
        int cnt = 0;
        try{
            if(argslst != null){
                for(int m=0;m<argslst.size();m++){
                    String[] argsarray = argslst.get(m);
                    matrixid = argsarray[0];
                    matrixFieldid = argsarray[1];
                    String sql = "";
                    if(rs.getDBType().equals("oracle")){
                        sql = "select id, subcompanyfield, deptField, objid from workflow_groupdetail where (subcompanyfield is not null and 'a'||subcompanyfield<>'a')";              
                    }else{
                        sql = "select id, subcompanyfield, deptField, objid from workflow_groupdetail where (subcompanyfield is not null and subcompanyfield<>'')";
                    }
                    rs.executeSql(sql);
                    while (rs.next()) {
                        String gpdtlid = Util.null2String(rs.getString("id"));
                        String resfield = Util.null2String(rs.getString("subcompanyfield"));
                        String objid = Util.null2String(rs.getString("objid"));
                        String[] customFieldInfoArray = Util.TokenizerString2(resfield, "[_]");
                        if (customFieldInfoArray.length < 3) continue;
                        
                        String customFieldName = "";
                        for (int i=1; i<customFieldInfoArray.length-1; i++) {
                            if (i == 1) {
                                customFieldName = customFieldInfoArray[i];
                            } else {
                                customFieldName += "_" + customFieldInfoArray[i];
                            }
                            
                        }
                        
                        if(customFieldName == null || "".equals(customFieldName)){
                            continue;
                        }
                        
                        
                        String matrixfieldidsql = "select id from MatrixFieldInfo WHERE fieldname='" + customFieldName + "' and matrixid=" + matrixid;
                        temprs.executeSql(matrixfieldidsql);
                        cnt++;
                        /*
                        if (temprs.next()) {
                            String matrixfieldid = Util.null2String(temprs.getString("id"));
                            GroupDetailMatrix gdm = new GroupDetailMatrix();
                            gdm.setGroupdetailid(gpdtlid);
                            gdm.setMatrix(matrixid);
                            gdm.setValue_field(matrixfieldid);
                            rs1.executeSql("select * from workflow_groupdetail_matrix where groupdetailid ='"+gpdtlid+"'");
                            if(rs1.next()){
                                gdm.update(temprs);
                            }else{
                                gdm.save(temprs);
                            }
                            
                            GroupDetailMatrixDetail gdmd = new GroupDetailMatrixDetail();
                            gdmd.setGroupdetailid(gpdtlid);
                            gdmd.setCondition_field(matrixFieldid);
                            gdmd.setWorkflow_field(objid);
                            rs1.executeSql("select * from workflow_matrixdetail where groupdetailid ='"+gpdtlid+"'");
                            if(rs1.next()){
                                rs1.executeSql("update workflow_matrixdetail set condition_field='"+matrixFieldid+"',workflow_field='"+objid+"' where groupdetailid ='"+gpdtlid+"'");
                            }else{
                                gdmd.save(temprs);
                            }
                            
                            String updateSql = "update workflow_groupdetail set type=99 where id=" + gpdtlid;
                            rslog.writeLog("20161121 ========> updateSql = "+updateSql);
                            temprs.executeSql(updateSql);
                        }
                        */
                        
                    }
                }
            }
        }catch(Exception e){
            e.printStackTrace();
        }
        if(cnt > 0){
            result = resultNeedFix;
            resultList.add("有[" + cnt + "]条数据需要修复");
        }else{
            result = resultOk;
        }
        return formatReturnJsonCheck(result, resultList,this);
    }

    @Override
    public JSONObject fixData(HashMap dataMap) {
        setRecordSet();
        (new BaseBean()).writeLog("HtmlInfoCheck  start");

        RTXClientCom rtxClient = new RTXClientCom();
        RecordSet rs = new RecordSet();
        RecordSet rs1 = new RecordSet();
        RecordSet rslog = new RecordSet();
        RecordSet temprs = new RecordSet();
        List resultList = new ArrayList();
        String result = "";
        List<String[]> argslst = new ArrayList<String[]>();

        argslst = MatrixUtil.getMatrixIDAndFieldID();

        String matrixid = "";
        String matrixFieldid = "";
        try{
        if(argslst != null){
            for(int m=0;m<argslst.size();m++){
                String[] argsarray = argslst.get(m);
                matrixid = argsarray[0];
                matrixFieldid = argsarray[1];
                String sql = "";
                if(rs.getDBType().equals("oracle")){
                    sql = "select id, subcompanyfield, deptField, objid from workflow_groupdetail where (subcompanyfield is not null and 'a'||subcompanyfield<>'a')";              
                }else{
                    sql = "select id, subcompanyfield, deptField, objid from workflow_groupdetail where (subcompanyfield is not null and subcompanyfield<>'')";
                }
                rs.executeSql(sql);
                while (rs.next()) {
                    String gpdtlid = Util.null2String(rs.getString("id"));
                    String resfield = Util.null2String(rs.getString("subcompanyfield"));
                    String objid = Util.null2String(rs.getString("objid"));
                    String[] customFieldInfoArray = Util.TokenizerString2(resfield, "[_]");
                    if (customFieldInfoArray.length < 3) continue;
                    
                    String customFieldName = "";
                    for (int i=1; i<customFieldInfoArray.length-1; i++) {
                        if (i == 1) {
                            customFieldName = customFieldInfoArray[i];
                        } else {
                            customFieldName += "_" + customFieldInfoArray[i];
                        }
                    }
                    if(customFieldName == null || "".equals(customFieldName)){
                        continue;
                    }
                    
                    String matrixfieldidsql = "select id from MatrixFieldInfo WHERE fieldname='" + customFieldName + "' and matrixid=" + matrixid;
                    temprs.executeSql(matrixfieldidsql);
                    if (temprs.next()) {
                        String matrixfieldid = Util.null2String(temprs.getString("id"));
                        GroupDetailMatrix gdm = new GroupDetailMatrix();
                        gdm.setGroupdetailid(gpdtlid);
                        gdm.setMatrix(matrixid);
                        gdm.setValue_field(matrixfieldid);
                        rs1.executeSql("select * from workflow_groupdetail_matrix where groupdetailid ='"+gpdtlid+"'");
                        if(rs1.next()){
                            gdm.update(temprs);
                        }else{
                            gdm.save(temprs);
                        }
                        
                        GroupDetailMatrixDetail gdmd = new GroupDetailMatrixDetail();
                        gdmd.setGroupdetailid(gpdtlid);
                        gdmd.setCondition_field(matrixFieldid);
                        gdmd.setWorkflow_field(objid);
                        rs1.executeSql("select * from workflow_matrixdetail where groupdetailid ='"+gpdtlid+"'");
                        if(rs1.next()){
                            rs1.executeSql("update workflow_matrixdetail set condition_field='"+matrixFieldid+"',workflow_field='"+objid+"' where groupdetailid ='"+gpdtlid+"'");
                        }else{
                            gdmd.save(temprs);
                        }
                        
                        String updateSql = "update workflow_groupdetail set type=99 where id=" + gpdtlid;
                        temprs.executeSql(updateSql);
                    }
                    
                }
            }
        }
        }catch(Exception e){
            e.printStackTrace();
        }
        needReload = "1";
        return checkData(new HashMap());
        
    }

    @Override
    public String getClassName() {
        return "矩阵还是显示E7的样式检测";
    }

    @Override
    public String getClassDesc() {
        return "E7升级E8时，遇到节点操作者中设置部门或者分部矩阵，还是显示E7的样式时修复";
    }

}



